<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cash extends CI_Controller {
	
	//申请提现页面
	function index(){
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if($user_id && $token == $token_val){
			$this->load->model('User_model', 'user');
			$this->load->model('Account_bank_model', 'bank');
			$this->load->model('Account_model', 'account');
			$this->load->model('Accountlog_model', 'accountlog');
			$user = $this->user->get_by_uid($user_id);
			if($user['real_status'] != 1){
				$ret['msg'] = '尚未实名认证';
				$ret['code'] = 2;
				echo json_encode($ret);exit;
			}
			$retUser = array(
				'user_id' => $user['user_id'],
				'username' => $user['username'],
				'email' => $user['email'],//email
				'realname' => $user['realname'],//真实姓名
			);
			
			$account = $this->account->get_account($user['user_id']);
			$retUser['use_money'] = $account['use_money'];//用户账户可用资金
			
			$bankList = $this->bank->arrays('var_account_bank');
			$bank = $this->bank->get_by_uid($user['user_id']);
			if (!isset($bank['user_id'])){
				$ret['msg'] = '尚未填写银行账户';
				$ret['code'] = 2;
				echo json_encode($ret);exit;
			}else{
				//用户绑定银行名称
				$retUser['bank'] = isset($bankList[$bank['bank']]) ? $bankList[$bank['bank']] : '';
				$retUser['bank_account'] = $bank['account'];//用户绑定银行账户
			}
			
			$cash_fee = $this->accountlog->cash_fee($user['user_id']);
			$retUser['cash_fee'] = min($cash_fee,$account['use_money']);//可免费提现金额
			
			
			$data['user'] = $retUser;
			$data['code'] = 0;
			echo json_encode($data);exit;
		}else{
			$ret['code'] = 1;
			$ret['msg'] = '请先登录';
			echo json_encode($ret);exit;
		}
	}
	
	//申请提现操作提交
	function submit(){
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if($user_id && $token == $token_val){
			$this->load->model('User_model', 'user');
			$this->load->model('Account_bank_model', 'bank');
			$this->load->model('Account_model', 'account');
			$this->load->model('Accountlog_model', 'accountlog');
			
			$data['user'] = $this->user->get_by_uid($user['user_id']);
			$data['bank_list'] = $this->bank->arrays('var_account_bank');
			$data['account'] = $this->account->get_account($user['user_id']);
			$data['bank'] = $this->bank->get_by_uid($user['user_id']);
			$data['cash_fee'] = $this->accountlog->cash_fee($user['user_id']);
			$data['cash_fee'] = min($data['cash_fee'],$data['account']['use_money']);
			
			if($data['user']['real_status'] != 1){
				$ret['msg'] = '尚未实名认证';
				$ret['code'] = 2;
				echo json_encode($ret);exit;
			}elseif($data['user']['paypassword'] == ''){
				$ret['msg'] = '尚未设置交易密码';
				$ret['code'] = 4;
				echo json_encode($ret);exit;
			}elseif (!isset($data['bank']['user_id'])){
				$ret['msg'] = '尚未填写银行账户';
				$ret['code'] = 3;
				echo json_encode($ret);exit;
			}else{
				$paypwd = $this->input->get_post('paypwd');//交易密码
				$money  = $this->input->get_post('money');//申请提现金额
				if(!is_numeric($money)){
					$ret['msg'] = '无效的金额';
					$ret['code'] = 5;
					echo json_encode($ret);exit;
				}else{
					$money = round($money,2);
					if($money < 100 || $money > 1000000){
						$ret['msg'] = '单笔提现金额超出范围(100-1000000)';
						$ret['code'] = 6;
						echo json_encode($ret);exit;
					}elseif(md5(md5($paypwd)) != $data['user']['paypassword']){
						$ret['msg'] = '交易密码不正确';
						$ret['code'] = 7;
						echo json_encode($ret);exit;
					}elseif ($data['account']['use_money'] < $money){
						$ret['msg'] = '可用余额不足';
						$ret['code'] = 8;
						echo json_encode($ret);exit;
					}else{
						$time = time();
						$ip = $this->input->ip_address();
						$fee = 0;
						if($data['cash_fee'] < $money){
							$fee = round(($money - $data['cash_fee']) * 0.005,2);
						}
						$this->db->trans_begin();//事务开始
						//添加提现表记录
						$setArr = array(
							'user_id' => $user['user_id'],
							'fstatus' => '0',
							'account' => $data['bank']['account'],
							'bank' => $data['bank']['bank'],
							'branch' => $data['bank']['branch'],
							'province' => $data['bank']['province'],
							'city' => $data['bank']['city'],
							'total' => $money,
							'credited' => $money - $fee,
							'fee' =>$fee,
							'add_time' => $time,
							'addip' => $ip
						);
						$this->load->model('Accountcash_model', 'cash');
						$ret = $this->cash->add($setArr);
						if($ret > 0){
							$log = array(
								'user_id' => $user['user_id'],
								'ftype' => CASH_FROST,
								'money' => $money,
								'total' => $data['account']['total'],
								'use_money' => $data['account']['use_money']-$money,
								'no_use_money' => $data['account']['no_use_money']+$money,
								'collection' => $data['account']['collection'],
								'to_user' => 0,
								'add_time' => $time,
								'addip' => $ip,
								'remark' => "提现冻结"
							);
							$ret = $this->account->addlog($log);
						}
						if($ret){
							$this->db->trans_commit();
							$ret1['msg'] = '提现申请已提交，请等待审核';
							$ret1['code'] = 0;
							echo json_encode($ret1);exit;
						}else{
							$this->db->trans_rollback();
							$ret1['msg'] = '提现失败，请重试';
							$ret1['code'] = 9;
							echo json_encode($ret1);exit;
						}
					}
				}
			}
		}else{
			$ret['code'] = 1;
			$ret['msg'] = '请先登录';
			echo json_encode($ret);exit;
		}
	}
}